package com.rzico.market.plugin.phoneBill;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 支付 工具类
 */
public class DXPhoneHttpUtil {

    /**
     * 请求 支付接口
     * @param requestUrl 请求地址
     * @param xml 请求参数
     * @return 返回报文
     */
    public static String requestPay(String requestUrl, String xml) {
        // 创建SSLContext
        StringBuffer buffer = null;
        try{
            URL url = new URL(requestUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setDoInput(true);
           //System.out.println("连接前..................");
            conn.connect();
            //System.out.println("连接后....................");
            //往服务器端写内容
            if(null != xml){
                OutputStream os=conn.getOutputStream();
                os.write(xml.getBytes("utf-8"));
                os.close();
            }
            // 读取服务器端返回的内容
            //System.out.println("获取流之前................");
            InputStream is = conn.getInputStream();
            InputStreamReader isr = new InputStreamReader(is, "utf-8");
            BufferedReader br = new BufferedReader(isr);
            buffer = new StringBuffer();
            String line = null;
            while ((line = br.readLine()) != null) {
                buffer.append(line);
            }
            //System.out.println("得到流buffer......................");
            br.close();
        }catch(Exception e){
            return null;
        }
        return buffer.toString();
    }

}
